package com.youku.plugins.toolboxV4.view
{
   import flash.display.Sprite;
   import com.youku.baseUI.YoukuTextField;
   import fl.controls.RadioButtonGroup;
   import flash.utils.Dictionary;
   import com.youku.baseUI.ButtonInfo;
   import flash.utils.Timer;
   import com.youku.plugins.toolboxV4.ToolboxFont;
   import fl.controls.ButtonLabelPlacement;
   import flash.events.MouseEvent;
   import flash.events.Event;
   import com.youku.utils.AssetUtil;
   import flash.text.TextFieldAutoSize;
   import flash.events.TimerEvent;
   import com.youku.utils.ReportUtil;
   
   public class DefaultQualityPanel extends Sprite
   {
      
      public function DefaultQualityPanel() {
         super();
         this.init();
      }
      
      private var _title:YoukuTextField;
      
      private var _tbGroup:RadioButtonGroup;
      
      private var _tbDefer:Array;
      
      private var _tbSelectors:Dictionary;
      
      private var _buttonInfoDict:Dictionary;
      
      private var _buttonInfo:ButtonInfo;
      
      private var _buttonInfoTimer:Timer;
      
      private function init() : void {
         var _loc2_:ToolBoxRadioButton = null;
         this._tbDefer = [];
         this._tbSelectors = new Dictionary();
         this._tbGroup = new RadioButtonGroup("defaultQuality");
         this._tbDefer.push(
            {
               "type":"auto",
               "name":"自动"
            });
         this._tbDefer.push(
            {
               "type":"high",
               "name":"超清"
            });
         this._tbDefer.push(
            {
               "type":"mid",
               "name":"高清"
            });
         this._tbDefer.push(
            {
               "type":"low",
               "name":"标清"
            });
         var _loc1_:Number = 0;
         while(_loc1_ < this._tbDefer.length)
         {
            _loc2_ = new ToolBoxRadioButton();
            _loc2_.group = this._tbGroup;
            _loc2_.costomTextFormat = ToolboxFont.FONT_SELECTOR;
            _loc2_.labelPlacement = ButtonLabelPlacement.RIGHT;
            _loc2_.text = this._tbDefer[_loc1_].name;
            _loc2_.fontHoffset = -2;
            _loc2_.label = "";
            _loc2_.value = this._tbDefer[_loc1_].type;
            _loc2_.addEventListener(MouseEvent.MOUSE_OVER,this.onMouseOver);
            _loc2_.addEventListener(MouseEvent.MOUSE_OUT,this.onMouseOut);
            this._tbSelectors[this._tbDefer[_loc1_].type] = _loc2_;
            _loc2_.addEventListener(MouseEvent.CLICK,this.onSelectorClick);
            _loc1_++;
         }
         this._tbGroup.addEventListener(Event.CHANGE,this.onQualityChange);
         this._title = AssetUtil.getYoukuTextField(false);
         this._title.autoSize = TextFieldAutoSize.LEFT;
         this._title.defaultTextFormat = ToolboxFont.FONT_COMMON;
         this._title.fontHoffset = -2;
         this._title.text = "优先画质:";
         this.addChild(this._title);
         this._buttonInfoDict = new Dictionary();
         this._buttonInfoDict[this._tbSelectors["auto"]] = "根据带宽等情况自动匹配";
         this._buttonInfoDict[this._tbSelectors["high"]] = "4M带宽以上推荐";
         this._buttonInfoDict[this._tbSelectors["mid"]] = "2M带宽以上推荐";
         this._buttonInfoDict[this._tbSelectors["low"]] = "对带宽要求最低";
         this._buttonInfoTimer = new Timer(2 * 1000,1);
         this._buttonInfoTimer.addEventListener(TimerEvent.TIMER_COMPLETE,this.onButtonInfoTimeout);
         this._buttonInfo = new ButtonInfo();
         this._buttonInfo.visible = false;
         this.addChild(this._buttonInfo);
         this.resetOptions();
      }
      
      private function onSelectorClick(param1:MouseEvent) : void {
         var _loc2_:ToolBoxRadioButton = param1.target as ToolBoxRadioButton;
         switch(_loc2_.value as String)
         {
            case "high":
               ReportUtil.sendUserActionLog(ReportUtil.SETTINGS_PANEL_QUALITY_SUPER_DEFINITION);
               break;
            case "mid":
               ReportUtil.sendUserActionLog(ReportUtil.SETTINGS_PANEL_QUALITY_HIGH_DEFINITION);
               break;
            case "low":
               ReportUtil.sendUserActionLog(ReportUtil.SETTINGS_PANEL_QUALITY_NORMAL_HIGH_DEFINITION);
               break;
            case "auto":
               ReportUtil.sendUserActionLog(ReportUtil.SETTINGS_PANEL_QUALITY_AUTO_ADJUST);
               break;
         }
      }
      
      private function onQualityChange(param1:Event) : void {
         var _loc2_:ToolBoxRadioButton = null;
         for each(_loc2_ in this._tbSelectors)
         {
            if(_loc2_ == this._tbGroup.selection)
            {
               _loc2_.selected = true;
            }
            else
            {
               _loc2_.selected = false;
            }
         }
      }
      
      private function onMouseOver(param1:MouseEvent) : void {
         var _loc2_:String = this._buttonInfoDict[param1.target];
         if(!_loc2_)
         {
            return;
         }
         var _loc3_:Number = param1.target.x + param1.target.width / 2 + 5;
         var _loc4_:Number = 0;
         this._buttonInfo.setData(_loc2_,198,_loc3_ - this._tbSelectors["auto"].x);
         this._buttonInfo.x = _loc3_;
         this._buttonInfo.y = _loc4_;
         this._buttonInfo.visible = true;
         this._buttonInfoTimer.delay = 2 * 1000;
         this._buttonInfoTimer.reset();
         this._buttonInfoTimer.start();
      }
      
      private function onMouseOut(param1:MouseEvent) : void {
         this._buttonInfoTimer.stop();
         this._buttonInfo.visible = false;
      }
      
      private function onButtonInfoTimeout(param1:TimerEvent) : void {
         this._buttonInfo.visible = false;
         this._buttonInfoTimer.stop();
      }
      
      public function show() : void {
      }
      
      private function updateLayout() : void {
         this._title.x = 11;
         this._title.y = 4;
         this._tbSelectors["auto"].setWH(44,18);
         this._tbSelectors["auto"].x = this._title.x + this._title.width + 6;
         this._tbSelectors["auto"].y = 0;
         this._tbSelectors["high"].setWH(44,18);
         this._tbSelectors["high"].x = this._tbSelectors["auto"].x + this._tbSelectors["auto"].width + 12;
         this._tbSelectors["high"].y = 0;
         this._tbSelectors["mid"].setWH(44,18);
         this._tbSelectors["mid"].x = this._tbSelectors["high"].x + this._tbSelectors["high"].width;
         this._tbSelectors["mid"].y = 0;
         this._tbSelectors["low"].setWH(44,18);
         this._tbSelectors["low"].x = this._tbSelectors["mid"].x + this._tbSelectors["mid"].width;
         this._tbSelectors["low"].y = 0;
      }
      
      public function setDefaultQuality(param1:String) : void {
         switch(param1)
         {
            case "low":
               this._tbSelectors["low"].selected = true;
               break;
            case "mid":
               this._tbSelectors["mid"].selected = true;
               break;
            case "high":
               this._tbSelectors["high"].selected = true;
               break;
            case "auto":
               this._tbSelectors["auto"].selected = true;
               break;
         }
      }
      
      private function resetOptions() : void {
         var _loc1_:ToolBoxRadioButton = null;
         for each(_loc1_ in this._tbSelectors)
         {
            if(contains(_loc1_))
            {
               removeChild(_loc1_);
            }
         }
         addChild(this._tbSelectors["auto"]);
         addChild(this._tbSelectors["high"]);
         addChild(this._tbSelectors["mid"]);
         addChild(this._tbSelectors["low"]);
         this.updateLayout();
      }
      
      public function getDefaultQuality() : String {
         var _loc1_:* = "low";
         if(this._tbGroup.selection)
         {
            _loc1_ = this._tbGroup.selection.value as String;
         }
         return _loc1_;
      }
   }
}
